//
//  HomePageSmallCardVC.swift
//  首页 - 小卡位
//
//  Created by 千策万划-mini on 2025/4/30.
//

import Foundation

class HomePageSmallCardVC: BaseTableVC {
    
    var blockRefreshHome:(() -> Void)?
    var modelDetail: ModelHome = ModelHome()
    var blockApplyClick:((String) -> Void)?
    
    // MARK: - lazy
    lazy var viewContent: SmallCardContentView = {
        let result = SmallCardContentView()
        return result
    }()
    
    // MARK: - viewDidLoad
    override func viewDidLoad() {
        super.viewDidLoad()
        
        configUI()
        addRefresh()
    }
    
    override func addRefresh() {
        addRefreshHeader()
    }
    
    func configUI() {
        tableView.register(SmallCardProductCell.self, forCellReuseIdentifier: String(reflecting: SmallCardProductCell.self))
        view.backgroundColor = .clear
        viewBg.backgroundColor = .clear
        tableView.backgroundColor = .clear
        tableView.tableHeaderView = viewContent
        tableView.tableFooterView = { () -> UIView in
            let frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: W(40))
            let newModel = UIView(frame: frame)
            return newModel
        }()
    }
    
    // MARK: - tableView 代理
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return modelDetail.difference.impulsive.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: String(reflecting: SmallCardProductCell.self), for: indexPath) as! SmallCardProductCell
        cell.resetCellWithModel(modelDetail.difference.impulsive[indexPath.row])
        cell.blockApplyClick = { [weak self](cellMark) in
            guard let self = self else { return }
            guard let indexPath = self.tableView.indexPath(for: cellMark) else { return }
            if let block = blockApplyClick {
                block(UnPackStr(self.modelDetail.difference.impulsive[indexPath.row].gnawed))
            }
        }
        return cell
    }
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return SmallCardProductCell.fetchHeight(model: modelDetail.difference.impulsive[indexPath.row], className: nil, selectorName: "resetCellWithModel:")
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        feedback()
    }
    
    // MARK: - request
    override func requestList() {
        endRefreshing()
        if let block = blockRefreshHome {
            block()
        }
    }
}
